﻿using System;
using System.Data;
using System.Data.Common;

namespace Inaction.Business.Security
{
    [Serializable]
    public class QueryTableCommentCommand : Inaction.SqlCommand 
    {
        DataTable data = null;

        public DataTable Data
        {
            get { return data; }
        }

        string tableName = string.Empty;


        public QueryTableCommentCommand(string tableName = "")
        {
            this.tableName = tableName;
        }
        protected override void DoExecute(IDbConnection connection)
        {
            string sqlStr = @"SELECT DISTINCT 
                                    USER_TABLES.TABLE_NAME     AS TABLENAME,
                                    USER_TAB_COMMENTS.COMMENTS AS COMMENTS
                                    FROM USER_TABLES
                                    JOIN USER_TAB_COMMENTS
                                    ON USER_TABLES.TABLE_NAME = USER_TAB_COMMENTS.TABLE_NAME  
                                    "
                + (string.IsNullOrEmpty(tableName) ? string.Empty : " WHERE USER_TABLES.TABLE_NAME = '" + tableName) + @"'  
                                    ORDER BY TABLENAME";
            using (var command = connection.CreateCommand())
            {
                command.CommandText = sqlStr;
                data = new DataTable();
                data.Columns.Add("TABLENAME");
                data.Columns.Add("COMMENTS");
                var reader = command.ExecuteReader();
                while (reader.Read())
                {
                    var row = data.NewRow();
                    row[0] = reader[0];
                    row[1] = reader[1];
                    data.Rows.Add(row);
                }
            }
        }

       
    }
}
